<?php
//require_once '../prepend.php';
require_once '../core/Exception/MException.php';
require_once 'PHPUnit2/Framework/TestCase.php';

/**
 * Create a class to test the exception
 */
class TestMException extends PHPUnit2_Framework_TestCase 
{
    public $var;

    const THROW_NONE    = 0;
    const THROW_CUSTOM  = 1;
    const THROW_DEFAULT = 2;

    function __construct($avalue = self::THROW_NONE) {

        switch ($avalue) {
            case self::THROW_CUSTOM:
                // throw custom exception
                throw new MException('1 is an invalid parameter', 5);
                break;

            case self::THROW_DEFAULT:
                // throw default one.
                throw new Exception('2 isnt allowed as a parameter', 6);
                break;

            default:
                // No exception, object will be created.
                $this->var = $avalue;
                break;
        }
    }
    
}


// Example 1
echo "<hr>Example 1<br>";
try {
    $o = new TestMException(TestMException::THROW_CUSTOM);
    
} catch (MException $e) {      // Will be caught
    echo "Caught my exception\n", $e;
    $e->customFunction();
} catch (Exception $e) {        // Skipped
    echo "Caught Default Exception\n", $e;
}

// Continue execution
var_dump($o);
echo "\n\n";

echo "<hr>Example 2<br>";
// Example 2
try {
    $o = new TestMException(TestMException::THROW_DEFAULT);
} catch (MException $e) {      // Doesn't match this type
    echo "Caught my exception\n", $e;
    $e->customFunction();
} catch (Exception $e) {        // Will be caught
    echo "Caught Default Exception\n", $e;
}

// Continue execution
var_dump($o);
echo "\n\n";

echo "<hr>Example 3<br>";
// Example 3
try {
    $o = new TestMException(TestMException::THROW_CUSTOM);
} catch (Exception $e) {        // Will be caught
    echo "Default Exception caught\n", $e;
}

// Continue execution
var_dump($o);
echo "\n\n";

echo "<hr>Example 4<br>";
// Example 4
try {
    $o = new TestMException();
} catch (Exception $e) {        // Skipped, no exception
    echo "Default Exception caught\n", $e;
}

echo "<hr>continue execution<br>";
// Continue execution
var_dump($o);
echo "\n\n";